package com.commonhelper.util.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * RecyclerView瀑布流布局的分割线
 * 适用于一行两个布局的(单 双  不一样)
 */
public class StaggeredGridLayoutDecoration extends RecyclerView.ItemDecoration{


    private static final String TAG = StaggeredGridLayoutDecoration.class.getName();
    private int space;
    private int columnCount;

    public StaggeredGridLayoutDecoration(int space, int columnCount) {
        this.space = space;
        this.columnCount = columnCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
//        if (parent.getChildViewHolder(view) instanceof HomeAdapter.FormalThreeVH) {}
        outRect.top = space;
        //瀑布流专属分割线
        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        /**
         * 根据params.getSpanIndex()来判断左右边确定分割线
         * 第一列设置左边距为space，右边距为space/2  （第二列反之）
         */
//        if (params.getSpanIndex() % 2 == 0) {
//            outRect.left = space;
//            outRect.right = space / 2;
//        } else {
//            outRect.left = space / 2;
//            outRect.right = space;
//        }
        outRect.left = space / 2;
        outRect.right = space/ 2;
    }
}